/**
 * @author: 子龙
 */
package com.fzl.lotterysystem.controller.handler;


import com.fzl.lotterysystem.common.errorcode.GlobalErrorCodeConstants;
import com.fzl.lotterysystem.common.exception.ControllerException;
import com.fzl.lotterysystem.common.exception.ServiceException;
import com.fzl.lotterysystem.common.pojo.CommonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;



@Slf4j
@RestControllerAdvice//捕获全局的异常
public class GlobalExceptionHandler {

    @ExceptionHandler(value = ServiceException.class)
    public CommonResult<?> ServiceException(ServiceException e) {
        //打印错误日志
        log.error(e.getMessage(), e);
        //构造错误
        return CommonResult.error(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(),
                e.getMessage());
    }

    @ExceptionHandler(value = ControllerException.class)
    public CommonResult<?> ControllerException(ControllerException e) {
        //打印错误日志
        log.error(e.getMessage(), e);
        //构造错误
        return CommonResult.error(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(),
                e.getMessage());
    }

    @ExceptionHandler(value = Exception.class)
    public CommonResult<?> exception(Exception e) {
        //打印错误日志
        log.error(e.getMessage(), e);
        //构造错误
        return CommonResult.error(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(),
                e.getMessage());
    }
}